"""
循环经济数据可视化模块
"""
import matplotlib.pyplot as plt
import os
from typing import Dict, List
from .economic import EconomicAnalyzer
from .environmental import EnvironmentalAnalyzer
from .optimization import OptimizationEngine

class CircularEconomyVisualizer:
    def __init__(self):
        self.economic_analyzer = EconomicAnalyzer()
        self.environmental_analyzer = EnvironmentalAnalyzer()
        self.optimization_engine = OptimizationEngine()
        
    def plot_value_analysis(self, product_type: str, quantities: List[int], save_path: str = None):
        """
        绘制产品价值分析图表
        参数:
            product_type: 产品类型
            quantities: 数量列表
            save_path: 图片保存路径(可选)
        """
        values = []
        for qty in quantities:
            value = self.economic_analyzer.calculate_value(product_type, qty)
            values.append(value['total_value'])
        
        plt.figure(figsize=(10, 6))
        plt.plot(quantities, values, 'bo-')
        plt.title(f'{product_type} - 经济价值分析')
        plt.xlabel('数量')
        plt.ylabel('总价值(元)')
        plt.grid(True)
        
        if save_path:
            os.makedirs(os.path.dirname(save_path), exist_ok=True)
            plt.savefig(save_path)
            plt.close()
        else:
            plt.show()
    
    def plot_environmental_impact(self, product_type: str, quantities: List[int], save_path: str = None):
        """
        绘制环境影响分析图表
        参数:
            product_type: 产品类型
            quantities: 数量列表
            save_path: 图片保存路径(可选)
        """
        co2_savings = []
        material_savings = []
        
        for qty in quantities:
            impact = self.environmental_analyzer.assess_impact(product_type, qty)
            co2_savings.append(impact['total_co2'])
            material_savings.append(impact['material_saving'])
        
        plt.figure(figsize=(12, 6))
        
        plt.subplot(1, 2, 1)
        plt.plot(quantities, co2_savings, 'ro-')
        plt.title('CO2减排量')
        plt.xlabel('数量')
        plt.ylabel('CO2减排(kg)')
        plt.grid(True)
        
        plt.subplot(1, 2, 2)
        plt.plot(quantities, material_savings, 'go-')
        plt.title('材料回收量')
        plt.xlabel('数量')
        plt.ylabel('可回收材料(kg)')
        plt.grid(True)
        
        plt.suptitle(f'{product_type} - 环境影响分析')
        
        if save_path:
            os.makedirs(os.path.dirname(save_path), exist_ok=True)
            plt.savefig(save_path)
            plt.close()
        else:
            plt.show()
    
    def plot_optimization_results(self, optimization_results: Dict, save_path: str = None):
        """
        绘制回收网络优化结果
        参数:
            optimization_results: 优化结果字典
            save_path: 图片保存路径(可选)
        """
        labels = ['现有设施', '建议新增']
        sizes = [
            optimization_results['现有设施'], 
            optimization_results['建议新增']
        ]
        
        plt.figure(figsize=(8, 8))
        plt.pie(sizes, labels=labels, autopct='%1.1f%%',
                shadow=True, startangle=90)
        plt.title(f"{optimization_results['地区类型']}地区回收设施分布")
        
        if save_path:
            os.makedirs(os.path.dirname(save_path), exist_ok=True)
            plt.savefig(save_path)
            plt.close()
        else:
            plt.show()